package com.blue.controller;

import com.blue.example.Product;
import com.blue.pojo.Admin;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;

/**
 * Created by Administrator on 2019/8/3.
 */
public class TomcatController implements Initializable {
    @FXML
    private HBox selectHb;
    @FXML
    private HBox installationHb;
    @FXML
    private HBox selfConfigHb;
    @FXML
    private Line projectLine1;
    @FXML
    private Line projectLine2;
    @FXML
    private Line projectLine3;
    @FXML
    private Circle projectCircle1;
    @FXML
    private Circle projectCircle2;
    private GridPane gridPane = null;
    private Product product = null;
    @FXML
    private StackPane mainContainer;
    @FXML
    private Label timeLabel;
    @FXML
    private AnchorPane projectAnchorPane;
    @FXML
    private ImageView adminAvatar;
    @FXML
    private Label adminName;

    private Admin admin;

    public static String tomcatpath;

    public static String tomcatport;

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public StackPane getMainContainer() {
        return mainContainer;
    }

    public void setMainContainer(StackPane mainContainer) {
        this.mainContainer = mainContainer;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //启一个线程，用来同步获取系统时间
        new Thread( new Runnable() {
            @Override
            public void run() {
                while (true) {
                    //获取系统当前时间
                    LocalDateTime now = LocalDateTime.now();
                    //格式化时间
                    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern( "yyyy年MM月dd日 HH:mm:ss" );
                    String timeString = dateTimeFormatter.format( now );
                    //启一个UI线程
                    Platform.runLater( new Runnable() {
                        @Override
                        public void run() {
                            //将格式化后的日期时间显示在标签上
                            timeLabel.setText( timeString );
                        }
                    } );
                    try {
                        Thread.sleep( 1000 );
                    } catch (InterruptedException e) {
                        System.err.println( "中断异常" );
                    }
                }
            }
        } ).start();

        //step process
        installationHb.getStyleClass().add( "dark-theme" );
        projectLine1.setStroke( Color.GREEN );
        projectLine1.setStrokeWidth( 4.0 );
        projectCircle1.setFill( Color.GREEN );
        projectLine2.setStroke( Color.GREEN );
        projectLine2.setStrokeWidth( 4.0 );
        projectLine3.setStroke( Color.GREEN );
        projectLine3.setStrokeWidth( 4.0 );
        projectCircle2.setFill( Color.GREEN );

        product = new Product();
        gridPane = product.addTomcat( "Tomcat" );
        AnchorPane.setTopAnchor( gridPane, 10.0 );
        projectAnchorPane.getChildren().addAll( gridPane );
        product.tomcatInstallation.setOnAction( (ActionEvent e) -> {
            product.installTomcat( "Tomcat" );
        } );
    }

    //退出系统
    public void logout() throws Exception {
        //关闭主界面
        Stage mainStage = (Stage) mainContainer.getScene().getWindow();
        mainStage.close();
        //弹出登录界面
        Stage loginStage = new Stage();
        loginStage.setTitle( "Admin Login of BLUE" );
        FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource( "/fxml/login.fxml" ) );
        Parent root = fxmlLoader.load();
        Scene scene = new Scene( root );
        scene.getStylesheets().add( "/css/style.css" );
        loginStage.setMaximized( true );
        loginStage.getIcons().add( new Image( "/img/logo.png" ) );
        loginStage.setScene( scene );
        loginStage.show();
    }


    public void addConfig(ActionEvent actionEvent) {
        tomcatpath = product.getTomcatpath();
        tomcatport = product.getTomcatPort();
        System.out.println( "current tomcat installing path :" + tomcatpath );
        System.out.println( "current tomcat installing port :" + tomcatport );
        //product = new ChangeProjectController().getProduct();
        /*StringBuffer selectFlags = ChangeProjectController.selectFlags;
        System.out.println( "ChangeProjectController. selectFlags :" + ChangeProjectController.selectFlags.toString() );
        product = new Product();
        product.setSelectFlags( selectFlags );
        System.out.println( "TomcatController. selectFlags :" + product.selectFlags.toString() );
        projectAnchorPane.getChildren().removeAll( gridPane );
        GridPane gridPane = product.addConfig();
        AnchorPane.setTopAnchor( gridPane, 10.0 );
        projectAnchorPane.getChildren().addAll( gridPane );*/
        try {
            installationHb.getStyleClass().removeAll( "dark-theme" );
            selfConfigHb.getStyleClass().add( "dark-theme" );
            switchView2( "changeConfig.fxml" );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void switchView2(String fileName) throws Exception {
        //清空原有内容
        mainContainer.getChildren().clear();
        BorderPane borderPane = new FXMLLoader( getClass().getResource( "/fxml/" + fileName ) ).load();
        mainContainer.getChildren().add( borderPane );
    }

    public void backProject(ActionEvent actionEvent) {
        try {
            installationHb.getStyleClass().removeAll( "dark-theme" );
            selectHb.getStyleClass().add( "dark-theme" );
            switchView2( "changeProject.fxml" );
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void tomcatInstalling(ActionEvent actionEvent) {
    }
}
